// pages/admin/notice/send/send.js
const app = getApp();

Page({
  // 在data中添加important字段
  data: {
    noticeInfo: {
      title: '',
      content: '',
      targetRoles: [],
      important: false  // 添加这一行
    },
    roleOptions: [
      { value: 'student', name: '学生', checked: false },
      { value: 'teacher', name: '教师', checked: false },
      { value: 'counselor', name: '辅导员', checked: false },
      { value: 'admin', name: '管理员', checked: false }
    ],
    isSubmitting: false
  },

  // 输入框内容变化处理
  inputChange: function(e) {
    const { field } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    this.setData({
      [`noticeInfo.${field}`]: value
    });
  },
  
  // 角色选择变化
  roleChange: function(e) {
    const index = e.currentTarget.dataset.index;
    const checked = e.detail.value;
    
    // 更新选中状态
    const roleOptions = this.data.roleOptions;
    roleOptions[index].checked = checked;
    
    // 更新选中的角色列表
    const targetRoles = roleOptions
      .filter(role => role.checked)
      .map(role => role.value);
    
    this.setData({
      roleOptions,
      'noticeInfo.targetRoles': targetRoles
    });
  },
  
  // 提交表单
  submitForm: function() {
    // 表单验证
    const { title, content, targetRoles } = this.data.noticeInfo;
    
    if (!title.trim()) {
      wx.showToast({
        title: '请输入通知标题',
        icon: 'none'
      });
      return;
    }
    
    if (!content.trim()) {
      wx.showToast({
        title: '请输入通知内容',
        icon: 'none'
      });
      return;
    }
    
    if (targetRoles.length === 0) {
      wx.showToast({
        title: '请选择接收角色',
        icon: 'none'
      });
      return;
    }
    
    // 设置提交状态
    this.setData({
      isSubmitting: true
    });
    
    // 获取服务器URL
    const serverUrl = app.globalData.serverUrl || 'http://localhost:3000';
    
    // 发送请求添加通知
    wx.request({
      url: `${serverUrl}/api/admin/notices`,
      method: 'POST',
      data: this.data.noticeInfo,
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      },
      success: (res) => {
        if (res.statusCode === 201 || res.statusCode === 200) {
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              // 返回上一页并刷新
              setTimeout(() => {
                const pages = getCurrentPages();
                const prevPage = pages[pages.length - 2];
                // 调用上一页的刷新方法
                if (prevPage && prevPage.loadNoticeList) {
                  prevPage.loadNoticeList(true);
                }
                wx.navigateBack();
              }, 2000);
            }
          });
        } else {
          wx.showToast({
            title: res.data.message || '发送失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('发送通知请求失败:', err);
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({
          isSubmitting: false
        });
      }
    });
  },
  
  // 取消发送
  cancelSend: function() {
    wx.navigateBack();
  },
  
  // 添加重要通知切换处理函数
  importantChange: function(e) {
    const checked = e.detail.value;
    this.setData({
      'noticeInfo.important': checked
    });
  },
});